package com.swi.api.consume;

import com.base.dto.PageContent;
import com.base.util.BeanCopyUtil;
import com.swi.dto.consume.CustInfoDto;
import com.swi.dto.consume.TypeInfoDto;
import com.swi.dto.page.Page;
import com.swi.entity.consume.CustInfo;
import com.swi.entity.consume.TypeInfo;
import com.swi.service.consume.ICustInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Shisan on 2017/8/22.
 * 支出管理
 */
@Service
public class ApiConsumeImpl implements ApiConsume{

    @Autowired
    private ICustInfoService custInfoService;

    @Override
    public int insertCustInfo(CustInfoDto custInfo) throws Exception {
        if(custInfo != null){
            return custInfoService.insertSelective(convertTo(custInfo));
        }
        return 0;
    }

    @Override
    public int updateCustInfo(CustInfoDto custInfo) {
        return custInfoService.updateByPrimaryKey(convertTo(custInfo));
    }

    @Override
    public int deleteCustInfo(String id) {
        return custInfoService.deleteByPrimaryKey(id);
    }

    @Override
    public CustInfoDto getCustInfoById(String id) {
        return convertFrom(custInfoService.selectByPrimaryKey(id));
    }

    @Override
    public Page getCustInfoPage(PageContent page) {
//        List<CustInfo> list = (List<CustInfo>) page.getResult();
//        if(list!=null){
//            page.setResult(list.stream().map(this::convertFrom).collect(Collectors.toList()));
//        }
//        return custInfoService.selectByPage(page);
        return null;
    }

    private CustInfo convertTo(CustInfoDto custInfo){
        if(custInfo==null) return null;
        CustInfo cust = new CustInfo();
        try {
            BeanCopyUtil.copy(custInfo,cust,(v)->{
                if(v != null && v instanceof TypeInfoDto){
                    TypeInfo typeInfo = new TypeInfo();
                    try {
                        BeanCopyUtil.copy(v,typeInfo,null);
                    } catch (Exception e) { e.printStackTrace();}
                    return typeInfo;
                }
                return null;
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cust;
    }
    private CustInfoDto convertFrom(CustInfo custInfo){
        if(custInfo==null) return null;
        CustInfoDto cust = new CustInfoDto();
        try {
            BeanCopyUtil.copy(custInfo,cust,(v)->{
                if(v != null && v instanceof TypeInfo){
                    TypeInfoDto typeInfo = new TypeInfoDto();
                    try {
                        BeanCopyUtil.copy(v,typeInfo,null);
                    } catch (Exception e) { e.printStackTrace();}
                    return typeInfo;
                }
                return null;
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cust;
    }
}
